﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using CellphoneRepair.DAL;
using CellphoneRepair.Entities;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;

namespace CellphoneRepair.Forms
{
    public partial class EditXinghao : DevExpress.XtraEditors.XtraUserControl
    {
        public EditXinghao()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            AddPinpai add = new AddPinpai();

            if (add.ShowDialog() == DialogResult.OK)
            {
                LoadPinpai();
            }
        }

        private void LoadPinpai()
        {
            navBarGroup1.ItemLinks.Clear();

            using (PinpaiBll bll = new PinpaiBll())
            {
                var list = bll.GetAll();

                foreach (var pinpai in list)
                {
                    NavBarItem item = new NavBarItem(pinpai.pinpai);
                    item.Tag = pinpai.Idpinpai;
                    item.LinkClicked += item_LinkClicked;
                    navBarGroup1.ItemLinks.Add(item);
                }
            }
        }

        private void item_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            LoadXinghao(e.Link.Item.Tag.ToString());
            idPinpai = e.Link.Item.Tag.ToString();
        }

        private void LoadXinghao(string toString)
        {
            using (XinghaoBll bll = new XinghaoBll())
            {
                Xinghao x = new Xinghao();

                var list = bll.GetByCondition("*", string.Format(" AND Idpinpai = '{0}'", toString));

                xinghaoBindingSource.DataSource = list;
            }
        }

        private string idPinpai = string.Empty;
        private EnumEditType editType { get; set; }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            gridView1.AddNewRow();
            editType = EnumEditType.Create;
            gridView1.ShowEditForm();
        }

        private void xinghaoBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            Xinghao xinghao = new Xinghao();

            xinghao.Idpinpai = idPinpai;

            xinghao.Idxinghao = Commons.GenerateIdByForeTag("XH");

            e.NewObject = xinghao;
        }

        private void navBarGroup1_ItemChanged(object sender, EventArgs e)
        {
        }

        private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            using (XinghaoBll bll = new XinghaoBll())
            {
                switch (editType)
                {
                    case EnumEditType.Create:
                        bll.Insert(xinghaoBindingSource.Current as Xinghao);
                        break;
                    case EnumEditType.Edit:
                        bll.Update(xinghaoBindingSource.Current as Xinghao);
                        break;
                }
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            editType = EnumEditType.Edit;
            gridView1.ShowEditForm();
        }

        private void EditXinghao_Load(object sender, EventArgs e)
        {
            LoadPinpai();
        }
    }
}